/**
 * 描述：地区管理
 * 作者：彭涛涛
 * 日期：2019/6/10
 * 版本：0.0.1
 **/

(function(E, KIT, RES) {
  "use strict";
  var __URL_ROOT = RES.URL_ROOT + "/area";

    //修改
  var _addOrUpdate = function(e) {
    var __grid = this;

    rapid.confirm("确定要保存吗？", function(yes) {
      if(yes) {

        rapid.post({
          url: __URL_ROOT + "/save",
          data: $.extend(true, {}, e.serializeJson()),
          success: function(result) {
            e.hide();

            rapid.success("保存成功。", function() {
              __grid.reload();
            }, false);
          }
        });
      }
    });
  };

  //endregion

  rapid.Page.create({
    render: {
      titleBar: "地区管理",
      header: E.view({cssClass: "fn-section r-row r-p-sm"},
        E.textbox({id: "txtSearch", mode: "search", tip: "名称...", cssClass: "r-col-6"})
      ),
      main: E.grid({id: "grd"})
    },
    methods: {
      _getGridFields_: function() {
        return [{
          title: "全称", width: 200,
          action: function(item) {
            return this.action({id: "name", text: item.attrib_02});
          }
        }, {
          title: "全称", name: "attrib_02", editor: {
            validator: {
              rules: {required: true}
            }
          }
        }, {
          title: "编号", name: "attrib_01", editor: {
            validator: {
              rules: {required: true}
            }
          }, width: 100

        }, {
          title: "简称", name: "attrib_03", editor: true, width: 150

        }, {
          title: "上级区域名称", name: "attrib_04_cn", width: 100
        }, {
          title: "上级区域编号", name: "attrib_04", width: 100, visible: false
        }, {
          title: "等级", name: "attrib_05", width: 100

        }, {
          title: "经度", name: "attrib_07", editor: true, width: 100

        }, {
          title: "纬度", name: "attrib_06", editor: true, width: 100

        }];
      },

      _initGridEvent_: function() {
        var __page = this;

        rapid("#grd").grid()
                     .on("loadSuccess", function(data) {
                       if(data.list.length > 0) {
                         var __first = data.list[0];

                         if(__first.attrib_05 === 1) {
                           __page.tag("pCode", "")
                                 .tag("pName", "")
                                 .tag("level", 2);

                         } else {
                           __page.tag("pCode", __first.attrib_04)
                                 .tag("pName", __first.attrib_04_cn)
                                 .tag("level", parseInt(__first.attrib_05) + 1);
                         }
                       }
                     })
                     .on("beforeAdd", function(dv) {
                       var pName = __page.tag("pName");
                       if(rapid.isEmpty(pName)) {
                         dv.ui("attrib_04_cn").val("此项为顶级");
                         dv.ui("attrib_04").val("0");
                         dv.ui("attrib_05").val("1");

                       } else {
                         dv.ui("attrib_04_cn").val(pName);
                         dv.ui("attrib_04").val(__page.tag("pCode"));
                         dv.ui("attrib_05").val(__page.tag("level"));
                       }
                     })
                     .on("add", _addOrUpdate)
                     .on("update", _addOrUpdate)
                     .on("remove", function(ids) {
                       var __grid = this;

                       rapid.post({
                         url: __URL_ROOT + "/delete",
                         data: "ids=" + ids,
                         success: function() {
                           rapid.success("删除成功。", function() {
                             __grid.reload();
                           }, false);
                         }
                       });
                     });

      },

      _initGrid_: function() {
        rapid("#grd").grid({
          height: "100%",
          addable: true,
          updatable: true,
          removable: false,
          detailed: true,
          quickView: false,

          history: true,
          dataSource: {
            url: __URL_ROOT + "/findByPcode"
          },
          pageable: true,
          keyFields: "conflict_id",
          paramFields: ["conflict_id"],
          fields: this._getGridFields_()
        });

        this._initGridEvent_();
      }
    }
  })(function() {
    var __page = this;

    __page.tag("pCode", "")
          .tag("pName", "")
          .tag("level", "");

    //查询
    rapid.ui("#txtSearch").on("submit", function() {
      var __value = this.val();

      // 如果没有具体输入查询地区的名字，则不记录此次请求
      rapid("#grd").grid().load({
        data: "pcode=" + __page.tag("pCode") + "&areaName=" + __value + "&isSearch=1"
      }, rapid.hasValue(__value));
    });

    __page._initGrid_();

    rapid("#grd").grid().on("name", function(e) {
      var __grd = rapid("#grd").grid(),
        __ui = __grd.selection()[0].ui(),
        __pcode = __ui.attrib_01.val();

      __grd.load({
        data: "pcode=" + __pcode + "&areaName=" + rapid.ui("#txtSearch").val() + "&isSearch=0"
      });
    }).load();

  }).init();

})(rapid.html.Element, rapid.kit, rapid.res);
